// Created from GroupGenerationTemplate
// For Decisions HL7 Engine
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using DecisionsFramework.Design.Properties;
using Decisions.HL7.DataTypes;
using ST = System.String;
using SI = System.String;
using IS = System.String;
using ID = System.String;
using SNM = System.String;

namespace Decisions.HL7.Message
{
    [DataContract]
	public class MEDICATION_HISTORY_CCR_I16 : AbstractGroup
    {
        /// -----------------------------
        /// -----------------------------
		[DataMember]
	    [PropertyClassification("108 ORC",108)]
  	    [HL7Field(HL7Version.v27, 108, HL7Repeat.False, HL7Required.True,"ORC")]
	    public ORC ORC {get;set;}

        /// -----------------------------
        /// -----------------------------
		[DataMember]
	    [PropertyClassification("109 MEDICATION_ORDER_DETAIL",109)]
  	    [HL7Field(HL7Version.v27, 109, HL7Repeat.False, HL7Required.False,"MEDICATION_ORDER_DETAIL_CCR_I16")]
	    public MEDICATION_ORDER_DETAIL_CCR_I16 MEDICATION_ORDER_DETAIL {get;set;}

        /// -----------------------------
        /// -----------------------------
		[DataMember]
	    [PropertyClassification("115 MEDICATION_ENCODING_DETAIL",115)]
  	    [HL7Field(HL7Version.v27, 115, HL7Repeat.False, HL7Required.False,"MEDICATION_ENCODING_DETAIL_CCR_I16")]
	    public MEDICATION_ENCODING_DETAIL_CCR_I16 MEDICATION_ENCODING_DETAIL {get;set;}

        /// -----------------------------
        /// -----------------------------
		[DataMember]
	    [PropertyClassification("121 MEDICATION_ADMINISTRATION_DETAIL",121)]
  	    [HL7Field(HL7Version.v27, 121, HL7Repeat.True, HL7Required.False,"MEDICATION_ADMINISTRATION_DETAIL_CCR_I16")]
	    public MEDICATION_ADMINISTRATION_DETAIL_CCR_I16[] MEDICATION_ADMINISTRATION_DETAIL {get;set;}

        /// -----------------------------
        /// -----------------------------
		[DataMember]
	    [PropertyClassification("126 CTI",126)]
  	    [HL7Field(HL7Version.v27, 126, HL7Repeat.True, HL7Required.False,"CTI")]
	    public CTI[] CTI {get;set;}



		#region Parser Helpers and Conrete Impls

        internal override void ConsumeTextSegmentsForGroup(Stack<string> availableData, HL7ParserOptions options) {

			ORC = PopSegmentParseAndCheckRequired<ORC>(availableData, options, "ORC");
			MEDICATION_ORDER_DETAIL = ProcessGroup<MEDICATION_ORDER_DETAIL_CCR_I16>(availableData, options, "MEDICATION_ORDER_DETAIL");
			MEDICATION_ENCODING_DETAIL = ProcessGroup<MEDICATION_ENCODING_DETAIL_CCR_I16>(availableData, options, "MEDICATION_ENCODING_DETAIL");
			MEDICATION_ADMINISTRATION_DETAIL = ProcessGroupArray<MEDICATION_ADMINISTRATION_DETAIL_CCR_I16>(availableData, options, "MEDICATION_ADMINISTRATION_DETAIL");
			CTI = PopSegmentParseAndCheckRequiredArray<CTI>(availableData, options, "CTI");
        }

		internal override bool CanConsume(string availableData, HL7ParserOptions options)
        {
			bool isORC = availableData.StartsWith("ORC", StringComparison.OrdinalIgnoreCase);
			if (IsRequired("ORC", options.Version) || isORC) {
				return isORC;
			}
			MEDICATION_ORDER_DETAIL_CCR_I16 testMEDICATION_ORDER_DETAIL_CCR_I16 = new MEDICATION_ORDER_DETAIL_CCR_I16();
			bool grpCCMEDICATION_ORDER_DETAIL_CCR_I16 = (testMEDICATION_ORDER_DETAIL_CCR_I16.CanConsume(availableData, options));
			if (IsRequired("MEDICATION_ORDER_DETAIL", options.Version) || grpCCMEDICATION_ORDER_DETAIL_CCR_I16) {
				return grpCCMEDICATION_ORDER_DETAIL_CCR_I16;
			}
			MEDICATION_ENCODING_DETAIL_CCR_I16 testMEDICATION_ENCODING_DETAIL_CCR_I16 = new MEDICATION_ENCODING_DETAIL_CCR_I16();
			bool grpCCMEDICATION_ENCODING_DETAIL_CCR_I16 = (testMEDICATION_ENCODING_DETAIL_CCR_I16.CanConsume(availableData, options));
			if (IsRequired("MEDICATION_ENCODING_DETAIL", options.Version) || grpCCMEDICATION_ENCODING_DETAIL_CCR_I16) {
				return grpCCMEDICATION_ENCODING_DETAIL_CCR_I16;
			}
			MEDICATION_ADMINISTRATION_DETAIL_CCR_I16 testMEDICATION_ADMINISTRATION_DETAIL_CCR_I16 = new MEDICATION_ADMINISTRATION_DETAIL_CCR_I16();
			bool grpCCMEDICATION_ADMINISTRATION_DETAIL_CCR_I16 = (testMEDICATION_ADMINISTRATION_DETAIL_CCR_I16.CanConsume(availableData, options));
			if (IsRequired("MEDICATION_ADMINISTRATION_DETAIL", options.Version) || grpCCMEDICATION_ADMINISTRATION_DETAIL_CCR_I16) {
				return grpCCMEDICATION_ADMINISTRATION_DETAIL_CCR_I16;
			}
			bool isCTI = availableData.StartsWith("CTI", StringComparison.OrdinalIgnoreCase);
			if (IsRequired("CTI", options.Version) || isCTI) {
				return isCTI;
			}
			return false;
        }

        private static Dictionary<string, bool> SEGMENT_VERSION_REQUIRED_MAP = new Dictionary<string, bool>();

		protected override Dictionary<string, bool> MAP_OF_SEGMENT_TO_REQUIRED
        {
            get
			{
				return SEGMENT_VERSION_REQUIRED_MAP;
			}
        }

		public override string HL7ENTITY_TYPE_NAME
        {
            get {
				return "MEDICATION_HISTORY";
			}
        }

        static MEDICATION_HISTORY_CCR_I16() {

             SetupStaticPropertyMap(SEGMENT_VERSION_REQUIRED_MAP, typeof(MEDICATION_HISTORY_CCR_I16));

        }

		#endregion

		public override string GetER7(HL7ParserOptions options)
		{
			List<string> segmentData = new List<string>();
		 if (ORC != null) {
			string segmentValue = ORC.GetER7(options);
			if (string.IsNullOrEmpty(segmentValue) == false) {
				segmentData.Add(segmentValue);
			}
		 }
		 if (MEDICATION_ORDER_DETAIL != null) {
			string segmentValue = MEDICATION_ORDER_DETAIL.GetER7(options);
			if (string.IsNullOrEmpty(segmentValue) == false) {
				segmentData.Add(segmentValue);
			}
		 }
		 if (MEDICATION_ENCODING_DETAIL != null) {
			string segmentValue = MEDICATION_ENCODING_DETAIL.GetER7(options);
			if (string.IsNullOrEmpty(segmentValue) == false) {
				segmentData.Add(segmentValue);
			}
		 }
		 if (MEDICATION_ADMINISTRATION_DETAIL != null && MEDICATION_ADMINISTRATION_DETAIL.Length > 0) {
			for (int i = 0; i < MEDICATION_ADMINISTRATION_DETAIL.Length; i++) {
				string segmentValue = MEDICATION_ADMINISTRATION_DETAIL[i].GetER7(options);
				if (string.IsNullOrEmpty(segmentValue) == false) {
					segmentData.Add(segmentValue);
				}
			}
		 }
		 if (CTI != null && CTI.Length > 0) {
			for (int i = 0; i < CTI.Length; i++) {
				string segmentValue = CTI[i].GetER7(options);
				if (string.IsNullOrEmpty(segmentValue) == false) {
					segmentData.Add(segmentValue);
				}
			}
		 }
           return string.Join(HL7ParserConstants.SEGMENT_SEPARATOR, segmentData.ToArray());
        
        }
    }
}

